package com.xh.bussiness.permission.controller;

import com.xh.bussiness.permission.bean.request.UserRoleRequest;
import com.xh.bussiness.permission.model.Role;
import com.xh.bussiness.permission.service.IUserRoleService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import com.xh.core.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Api(tags = "用户角色接口")
@RestController
@RequestMapping("/permission/userRole")
public class UserRoleController extends BaseController {

    @Autowired
    private IUserRoleService userRoleService;


    @ApiOperation(value = "查询用户角色",notes = "查询用户角色")
    @PostMapping("/queryAll")
    public AjaxResult queryAll(UserRoleRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", userRoleService.queryUserRole(bean));
    }

  
    @ApiOperation(value = "新增用户角色",notes = "新增用户角色")
    @PostMapping(value="/add")
    @Transactional
    public AjaxResult add(UserRoleRequest bean){
        String msg=userRoleService.addUserRole(bean);
        return new AjaxResult(msg==null,msg);
    }


    @ApiOperation(value = "修改用户角色",notes = "修改用户角色")
    @PostMapping(value="/modify")
    @Transactional
    public AjaxResult modify(UserRoleRequest bean){
        String msg=userRoleService.modifyUserRole(bean);
        return new AjaxResult(msg==null,msg);
    }


    @ApiOperation(value = "用户角色删除",notes = "用户角色删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户角色id",required = true,dataType = "String")
    })
    @PostMapping("/delete")
    @Transactional
    public AjaxResult delete(String id){
        String msg=userRoleService.deleteUserRole(id);
        return new AjaxResult(msg==null,msg);
    }

    @GetMapping("/getRole")
    public AjaxResult getRoleByUserId(){
        List<Role> roles = userRoleService.queryRoleByUserId(UserUtils.getCurrentUserId());
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, roles);
}
}

